package flc.ast.utils;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import java.util.Arrays;
import java.util.List;

import flc.ast.bean.PracticeTime;


public class DBEngine {
    private PracticeDao practiceDao;

    public DBEngine(Context context) {
        PracticeDatabase practiceDatabase = PracticeDatabase.getInstance(context);
        practiceDao = practiceDatabase.getPracticeDao();
    }

    public void insertPracticeTime(PracticeTime... practiceTimes) {
        new InsertAsyncTask(practiceDao).execute(practiceTimes);
    }

    public void updatePracticeTime(PracticeTime... practiceTimes) {
        new UpdateAsyncTask(practiceDao).execute(practiceTimes);
    }

    public void deletePracticeTime(PracticeTime... practiceTimes) {
        new DeleteAsyncTask(practiceDao).execute(practiceTimes);
    }

    public void deleteAllPracticeTime() {
        new DeleteAllAsyncTask(practiceDao).execute();
    }

    public void queryAllPracticeTime() {
        new QueryAllAsyncTask(practiceDao).execute();
    }

    public void queryTodayPracticeTime(long date) {
        new QueryTodayAsyncTask(practiceDao).execute();
    }


    //异步
    static class UpdateAsyncTask extends AsyncTask<PracticeTime, Void, Void> {
        private PracticeDao dao;

        public UpdateAsyncTask(PracticeDao practiceDao) {
            dao = practiceDao;
        }

        @Override
        protected Void doInBackground(PracticeTime... practiceTimes) {
            dao.updatePracticeTime(practiceTimes);
            return null;
        }
    }

    //条件删除
    static class DeleteAsyncTask extends AsyncTask<PracticeTime, Void, Void> {
        private PracticeDao dao;

        public DeleteAsyncTask(PracticeDao practiceDao) {
            dao = practiceDao;
        }

        @Override
        protected Void doInBackground(PracticeTime... practiceTimes) {
            dao.deletePracticeTime(practiceTimes);
            return null;
        }
    }

    //全部删除
    static class DeleteAllAsyncTask extends AsyncTask<Void, Void, Void> {
        private PracticeDao dao;

        public DeleteAllAsyncTask(PracticeDao practiceDao) {
            dao = practiceDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            dao.deleteAllPractice();
            return null;
        }
    }


    static class QueryAllAsyncTask extends AsyncTask<Void, Void, Void> {
        private PracticeDao dao;

        public QueryAllAsyncTask(PracticeDao practiceDao) {
            dao = practiceDao;
        }

        @Override
        protected Void doInBackground(Void... PracticeTimes) {
            List<PracticeTime> allPracticeTime = dao.getAllPractice();
            //遍历全部查询的结果
            for (PracticeTime PracticeTime : allPracticeTime) {
                Log.e("Hunter", PracticeTime.toString());
            }
            return null;
        }
    }

    static class QueryTodayAsyncTask extends AsyncTask<Long, Void, List<PracticeTime>> {
        private PracticeDao dao;

        public QueryTodayAsyncTask(PracticeDao practiceDao) {
            dao = practiceDao;
        }


        @Override
        protected List<PracticeTime> doInBackground(Long... longs) {
            List<PracticeTime> allPracticeTime = dao.getTodayPracticeTime(longs[0].longValue());
            return allPracticeTime;
        }
    }


    static class InsertAsyncTask extends AsyncTask<PracticeTime, Void, Void> {
        private PracticeDao dao;

        public InsertAsyncTask(PracticeDao practiceDao) {
            dao = practiceDao;
        }

        @Override
        protected Void doInBackground(PracticeTime... practiceTimes) {
            dao.insertPracticeTime(practiceTimes);
            return null;
        }
    }
}

